package com.yuwei.mine

import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.tabs.TabLayoutMediator
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.yuwei.base.RoutePath
import com.yuwei.common.mvi.BaseMviActivity
import com.yuwei.common.mvi.BaseViewActivity
import com.yuwei.common.mvi.UiState
import com.yuwei.database.entity.OrderEntity
import com.yuwei.mine.adapter.FragmentAdapter
import com.yuwei.mine.adapter.OrderStatusAdapter
import com.yuwei.mine.databinding.ActivityAllOrdersBinding
import com.yuwei.mine.entity.OrderStatusEntity


@Route(path = RoutePath.MODULE_MINE_ALL_ORDERS_ACTIVITY)
class AllOrdersActivity : BaseMviActivity<ActivityAllOrdersBinding,MineViewModel>() {
    lateinit var orderStatusAdapter: OrderStatusAdapter
    var list= listOf<OrderStatusEntity>()
    var listFragment= listOf<Fragment>()
    lateinit var fragmentAdapter: FragmentAdapter
    override fun successData(it: UiState.onSuccess<*>) {
        when(it.type){
            "getOrder"->{
                LogUtils.d((it.data as List<OrderEntity>).size)
            }
        }
    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        vm.sendIntent(MineIntent.getOrder)
        binding.ivBack.setOnClickListener {
            finish()
        }
        binding.llPop.setOnClickListener {
            XPopup.Builder(this)
                .atView(binding.llPop)
                .enableDrag(true)
                .popupAnimation(PopupAnimation.TranslateFromTop)
                .asCustom(object : BasePopupView(this) {
                    override fun getInnerLayoutId(): Int = R.layout.popup_order_check

                    override fun onCreate() {
                        super.onCreate()
                    }
                })
                .show()
        }
        binding.tvPermissions.setOnClickListener {
            ARouter.getInstance().build(RoutePath.MODULE_MINE_PERMISSIONS_ACTIVITY).navigation()
        }
         listFragment= listOf(
             ARouter.getInstance().build(RoutePath.MODULE_MINE_ORDER_FRAGMENT).navigation() as Fragment,
             ARouter.getInstance().build(RoutePath.MODULE_MINE_PAYING_FRAGMENT).navigation() as Fragment,
             ARouter.getInstance().build(RoutePath.MODULE_MINE_SHIPPED_FRAGMENT).navigation() as Fragment,
             ARouter.getInstance().build(RoutePath.MODULE_MINE_RECEIVED_FRAGMENT).navigation() as Fragment,
             ARouter.getInstance().build(RoutePath.MODULE_MINE_EVALUATED_FRAGMENT).navigation() as Fragment,
         )
        fragmentAdapter=FragmentAdapter(this,listFragment)
        binding.vp.adapter=fragmentAdapter
        val tabs=ArrayList<String>()
        tabs.add("全部")
        tabs.add("待支付")
        tabs.add("待发货")
        tabs.add("待收货")
        tabs.add("待评价")
        TabLayoutMediator(binding.tab,binding.vp) { tab, position ->
            tab.text = tabs[position]
        }.attach()
        binding.tvPermissions.setOnClickListener {
            ARouter.getInstance().build(RoutePath.MODULE_MINE_PERMISSIONS_ACTIVITY).navigation()
        }
    }

    override fun initData() {
        list= listOf(
           OrderStatusEntity("全部订单",true),
            OrderStatusEntity("商品购物",false),
            OrderStatusEntity("增值服务",false),
            OrderStatusEntity("生活服务",false),
            OrderStatusEntity("休闲娱乐",false),
            OrderStatusEntity("其他",false)
        )
        orderStatusAdapter= OrderStatusAdapter(list,binding.tvPop.text.toString())
        binding.rvStatus.layoutManager= LinearLayoutManager(this)
        binding.rvStatus.adapter=orderStatusAdapter
    }
}